import { observable, action } from "mobx";


import { Modal } from "antd";
const confirm = Modal.confirm;





export default class User{
  constructor(root){
    this.root = root;
  }
  
  // 刷新列表
  @observable loading=false;
  // 分页数据
  @observable paging={}

  
  // 查询数据
  @observable where={};
  // 用户表单数据
  @observable form = {};

  // 初始化
  @action init=()=>{
    this._init();
  }
  
  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){obj[temp]={};}
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        obj[temp] = v;
      }
    };
    loop(this,arr,v);
    switch(k){
      case "form-loading":
        this.root.set("loading",v);
        break;
      case "loading":
      default:
        this.root.set(k,v);
    }
  }
  
  // input赋值
  @action change=(e,field)=>{
    switch(field){
    case "form-data-visible":
      this.set(field,e?1:0);
      break;
    case "form-data-authority":
      this.set(field,e);
      break;
    default:
      this.set(field,e.target.value);
    }
    this.set("form-tip","");
  }

  // 显示所有
  @action all=()=>{
    this._init();
  }
  // 搜索
  @action find=()=>{
    this._paging(1);
  }
  
  // 添加编辑用户按钮事件
  @action modal=(form,record)=>{
    this._init_form();
    this.set("form-visible",true);
    this.set("form-condition",form);
    this.set("form-title",form==="update"?"编辑：用户编号-".concat(record.id):"新增用户");
    if(form === "update"){
      this.set("form-data-id",record.id);
      this.set("form-data-uname",record.uname);
      this.set("form-data-phone",record.phone);
      this.set("form-data-zfbUserId",record.zfbUserId);
      this.set("form-data-name",record.name);
    }
  }
  // 删除用户按钮事件
  @action delete=record=>{
    confirm({
      title: "删除确认",
      content: "是否真的删除 ".concat(record.uname,"?"),
      okText:"确认",
      cancelText:"取消",
      onOk:()=>{
        this.set("loading",true);
        this.root.ajax.post({
          url:this.root.url,
          data:{act:"admin-user-delete",id:record.id},
          success:json=>{
            this.set("loading",false);
            if(json.status !== "ok"){this.root.alert(json);return;}
            this._paging(this.paging.nowPage);
          }
        });
      }
    });
  }

  // 提交insert/update表单数据
  @action submit=()=>{
    if(this.form.condition==="insert"){
      if(!this.form.data.phone){this.set("form-tip","请输入电话号码");return;}
      if(!this.form.data.uname){this.set("form-tip","请输入用户名");return;}
      if(!this.form.data.pwd){this.set("form-tip","请输入密码");return;}
    }
    this.set("loading",true);
    this.set("form-loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-user-".concat(this.form.condition),
        phone:this.form.data.phone,
        uname:this.form.data.uname,
        pwd:this.form.data.pwd,
        visible:this.form.data.visible,
        id:this.form.data.id,
        authority:this.form.data.authority
      },
      success:json=>{
        this.set("loading",false);
        this.set("form-loading",false);
        if(json.status !== "ok"){
          this.set("form-tip",json.data);
        }else{
          if(this.form.condition==="update"){this.set("form-visible",false);}
          this._paging(this.paging.nowPage);
        }
      }
    });
  }
  
  
    @action page=p=>{
      this._paging(p);
    }

  // 用户列表分页
    _paging(p){
      this.set("loading",true);
      p = p?p:1;
      this.root.ajax.post({
        url:this.root.url,
        data:{act:"admin-user-paging",p:p,field:this.where.field, value:this.where.value, condition:this.where.condition},
        success:json=>{
          this.set("loading",false);
          if(json.status !== "ok"){this.root.alert(json);return;}
          this.set("paging",json.data);
        }
      });
    }
    // 初始化
    _init(){
      this._init_form();
      this._init_where();
      this._init_paging();
      this._paging(1);
    }
    // 用户表单初始化
    _init_form(){
      this.set("form",{
        visible:false,
        condition:"install", // insert|update
        title:"",
        tip:"",
        loading:false,
        data:{
          id:0,
          phone:"",
          visible:1,
          authority:"0",
          uname:"",
          pwd:""
        }// 表单内数据-id,phone,visible,authority,uname,pwd
      });
    }
    // 查询表单初始化
    _init_where(){
      this.set("where",{
        field:"uname",
        value:"",
        condition:"="
      });
    }



    // 分页数据初始化
    _init_paging(){
      this.paging={
        data:[],
        maxPage:1,
        page :1,
        maxNum:1,
        num:10,
      };
    }

}